FROM ubuntu:20.04 as builder
LABEL maintainer service@fisco.com.cn
LABEL forker haibin.l@linkingcloud.cn

WORKDIR /

ARG SOURCE_BRANCH
ARG TARGETARCH

ENV DEBIAN_FRONTEND=noninteractive \
    SOURCE=${SOURCE_BRANCH:-master-2.0}

RUN apt-get -q update \
      && apt-get install -qy --no-install-recommends \
        git clang make build-essential cmake libssl-dev zlib1g-dev ca-certificates \
        libgmp-dev flex bison patch \
      && ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
      && apt-get install -qy --no-install-recommends tzdata \
      && dpkg-reconfigure --frontend noninteractive tzdata \
      && rm -rf /var/lib/apt/lists/*

RUN git config --global url."https://ghproxy.com/https://github.com/".insteadOf "https://github.com/" \
      && git config --global url."https://ghproxy.com/https://raw.githubusercontent.com/".insteadOf "https://raw.githubusercontent.com/" \
      && git config --global url."https://ghproxy.com/https://codeload.github.com/".insteadOf "https://codeload.github.com/" \
      && git config --global protocol.https.allow always \
      && git config --global http.sslVerify false \
      && git clone https://github.com/FISCO-BCOS/FISCO-BCOS.git \
      && cd FISCO-BCOS \
      && git checkout ${SOURCE} \
      && if [ "${TARGETARCH}" != "amd64" ]; then cmake -DARCH_NATIVE=on .; else cmake .; fi \
      && make -j \
      && strip bin/fisco-bcos

FROM ubuntu:20.04
LABEL maintainer service@fisco.com.cn

RUN apt-get -q update \
    && apt-get install -qy --no-install-recommends libssl-dev zlib1g-dev \
    && ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && apt-get install -qy --no-install-recommends tzdata \
    && dpkg-reconfigure --frontend noninteractive tzdata \
    && rm -rf /var/lib/apt/lists/*

COPY --from=builder /FISCO-BCOS/bin/fisco-bcos /usr/local/bin/

EXPOSE 30300 20200 8545

ENTRYPOINT ["/usr/local/bin/fisco-bcos"]
CMD ["--version"]